#pragma once

#include "stdafx.h"
#include "../common/ScriptExtInterface.h"

//////////////////////////////////////////////////////////////////////////
// Loaders

enum Languages
{
	Perl = 1,
	Python = 2,
	MaxLanguageId
};

struct LanguageLoader
{
	HMODULE module;
	script_extension::FntypeSetWindbgInterface SetInterface;
	script_extension::FntypeRunScript RunScript;

	bool IsValid() const
	{
		return (module && SetInterface && RunScript);
	}
};

//////////////////////////////////////////////////////////////////////////
// utilities
class WindbgOutput
{
public:
	virtual void Out(const char* msg) = 0;
	virtual void Out(const wchar_t* msg) = 0;
	virtual int Err(const char* msg, int ret) = 0;
	virtual int Err(const wchar_t* msg, int ret) = 0;
};

#define CHECK_RET(expr) \
if (!(expr)) \
	return loadResult = out.Err("Error in " #expr "\n", GetLastError())

#define CHECK_RET_RES(expr, retVal) \
if (!(expr)) \
	return loadResult = out.Err("Error in " #expr "\n", retVal)

#define CHECK_BREAK(expr) \
if (!(expr)) \
	break

class RegKeyHolder
{
public:
	RegKeyHolder(): key(NULL) {}
	~RegKeyHolder()
	{
		Close();
	}

	void Close()
	{
		if (key)
		{
			RegCloseKey(key);
			key = NULL;
		}
	}

	operator HKEY()
	{
		return key;
	}

	HKEY* operator &()
	{
		return &key;
	}

private:
	HKEY key;
};

inline void MakeModuleName(const wchar_t* path, const wchar_t* lib, std::wstring& moduleName)
{
	moduleName = path;
	if ((moduleName.size() > 0) && (moduleName[moduleName.size() - 1] != '\\'))
		moduleName += '\\';
	moduleName += lib;
}


